<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>收货地址管理</title>
    <link href="/static/libs/bootstrap.css" rel="stylesheet">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        
        .navbar {
            background-color: #ff5000;
            color: white;
            padding: 10px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .navbar .logo {
            display: flex;
            align-items: center;
        }
        
        .navbar .logo span {
            margin-left: 10px;
        }
        
        .navbar a {
            color: white;
            text-decoration: none;
            margin: 0 10px;
        }
        
        .search-bar {
            display: flex;
            justify-content: center;
            margin: 20px 0;
        }
        
        .search-bar input {
            width: 500px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px 0 0 5px;
        }
        
        .search-bar button {
            padding: 10px;
            background-color: #ff5000;
            border: none;
            color: white;
            border-radius: 0 5px 5px 0;
        }
        
        .category-menu {
            background-color: #f5f5f5;
            padding: 10px;
            display: flex;
            justify-content: space-around;
        }
        
        .category-menu a {
            text-decoration: none;
            color: #333;
            padding: 10px 20px;
        }
        
        .content-box {
            display: flex;
            padding: 20px;
        }
        
        .left-nav, .right-content {
            float: left;
        }
        
        .left-nav {
            width: 20%;
            background-color: #f5f5f5;
            padding: 10px;
        }
        
        .left-nav a {
            display: block;
            text-decoration: none;
            color: #333;
            padding: 10px 20px;
            margin: 5px 0;
            background-color: #fff;
            border-radius: 5px;
        }
        
        .left-nav a.active, .left-nav a:hover {
            background-color: #ff5000;
            color: white;
        }
        
        .right-content {
            width: 75%;
            margin-left: 5%;
        }
        
        .right-title {
            font-size: 24px;
            margin-bottom: 20px;
        }
        
        .form-group {
            margin-bottom: 15px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        
        .form-group input, .form-group select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        
        .btn-submit {
            padding: 10px 20px;
            background-color: #ff5000;
            border: none;
            color: white;
            border-radius: 5px;
            cursor: pointer;
        }
        
        .btn-submit:hover {
            background-color: #e04c00;
        }
        
        .address-list {
            margin-top: 30px;
        }
        
        .address-list table {
            width: 100%;
            border-collapse: collapse;
        }
        
        .address-list th, .address-list td {
            padding: 10px;
            border: 1px solid #ccc;
            text-align: left;
        }
        
        .address-list th {
            background-color: #f5f5f5;
        }
        
        .necessary::after {
            content: "*";
            color: red;
            margin-left: 5px;
            font-size: 12px;
            font-weight: bold;
            vertical-align: middle;
            position: relative;
            top: -2px;
        }
    </style>
</head>
<body>
<div class="navbar">
    <div class="logo">机械工业</div>
    <div class="nav-links">
        <a id="index1Link" href="#">首页</a>
        <a id="userMessageLink" href="#">我的</a>
    </div>
</div>

<div class="search-bar">
    <input type="text" placeholder="搜索商品、店铺">
    <button>搜索</button>
</div>

<div class="content-box clear_fix">
    <div class="left-nav">
        <a href="#" class="active">收货地址管理</a>
        <a id="orderlistLink" href="#">我的订单</a>
        <a id="cartLink" href="#">我的购物车</a>
        <a id="information_modificationLink" href="#">个人资料</a>
        <a id="password_modificationLink" href="#">修改密码</a>
    </div>
    <div class="content" style="display: flex;flex-direction: column">
        <div class="btn btn-primary" style="width: 100px" id="add-address">新增地址</div>
        <table class="table">
            <thead>
            <tr>
                <th scope="col">ID</th>
                <th scope="col">收件人姓名</th>
                <th scope="col">收件人固定电话号码</th>
                <th scope="col">收件人手机号码</th>
                <th scope="col">省份</th>
                <th scope="col">城市</th>
                <th scope="col">县/区</th>
                <th scope="col">详细地址</th>
                <th scope="col">邮编</th>
                <th scope="col">操作</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
    
    <div class="modal" id="address-modal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="modal-title"></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="name-input" class="form-label necessary">收件人姓名</label>
                        <input type="text" class="form-control" id="name-input">
                    </div>
                    <div class="mb-3">
                        <label for="phone-input" class="form-label">收件人固定电话号码</label>
                        <input type="text" class="form-control" id="phone-input">
                    </div>
                    <div class="mb-3">
                        <label for="mobile-input" class="form-label necessary">收件人手机号码</label>
                        <input type="text" class="form-control" id="mobile-input">
                    </div>
                    <div class="mb-3">
                        <label for="province" class="necessary">省份：</label>
                        <select id="province" onchange="loadCities(this.value)">
                            <option>请选择省份</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="city" class="necessary">城市：</label>
                        <select id="city" onchange="loadDistricts(this.value)" disabled>
                            <option>请选择城市</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="district" class="necessary">区/县：</label>
                        <select id="district" disabled>
                            <option>请选择区/县</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="addr-input" class="form-label">详细地址</label>
                        <input type="text" class="form-control" id="addr-input">
                    </div>
                    <div class="mb-3">
                        <label for="zip-input" class="form-label necessary">邮编</label>
                        <input type="text" class="form-control" id="zip-input">
                    </div>
                    
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="submit">提交</button>
                </div>
            </div>
        </div>
    </div>

</div>
</body>
<script src="/static/libs/bootstrap.js"></script>
<script src="/static/js/axios.js"></script>
<script src="/static/js/city-selector.js"></script>

<script>

    function getUserIdFromUrl() {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get('id');
    }
    const userId = getUserIdFromUrl();
    const userMessageLink = document.getElementById("userMessageLink");
    const index1Link = document.getElementById("index1Link");
    const cartLink = document.getElementById("cartLink");
    const orderlistLink = document.getElementById("orderlistLink");
    const information_modificationLink = document.getElementById("information_modificationLink");
    const password_modificationLink = document.getElementById("password_modificationLink");
    userMessageLink.href = `userMessage?id=${userId}`;
    index1Link.href = `index1?id=${userId}`;
    cartLink.href =`cart?id=${userId}`;
    orderlistLink.href = `orderlist?id=${userId}`;
    information_modificationLink.href = `information_modification?id=${userId}`;
    password_modificationLink.href = ` password_modification?id=${userId}`;

    const modalTitle = document.querySelector("#modal-title");
    const addressModal = new bootstrap.Modal(document.getElementById("address-modal"));
    let currentChangeType = "insert";
    let currentId;
    const update = id => {
        modalTitle.innerText = "修改地址";
        currentId = id;
        currentChangeType = "update";
        addressModal.show();
        axios.get("http://localhost:8080/addresses/get?id=" + id).then(res => {
            document.querySelector("#name-input").value = res.data.name;
            document.querySelector("#phone-input").value = res.data.phone;
            document.querySelector("#mobile-input").value = res.data.mobile;
            document.querySelector("#province").value = res.data.province;
            document.querySelector("#province").onchange();
            document.querySelector("#city").value = res.data.city;
            document.querySelector("#city").onchange();
            document.querySelector("#district").value = res.data.district;
            document.querySelector("#addr-input").value = res.data.addr;
            document.querySelector("#zip-input").value = res.data.zip;
        })
    }
    const remove = id => {
        axios.delete("http://localhost:8080/addresses/delete?id=" + id).then(res => {
            window.location.reload();
        });
    }
    const setDefault = id => {
        axios.post("http://localhost:8080/addresses/setDefault?id=" + id).then(res => {
            window.location.reload();
        });
    }
    onload = () => {
        loadProvinces(''); // 传入空字符串以确保城市选择器被禁用

        const addAddressBtn = document.querySelector("#add-address");
        const submitBtn = document.querySelector("#submit");
        addAddressBtn.addEventListener("click", () => {
            modalTitle.innerText = "新增地址";
            addressModal.show();
            currentChangeType = "insert";
        })


        axios.get("http://localhost:8080/addresses/list?id=" + userId).then(res =>{
            res.data.forEach(item => {
                let template = `
        <tr>
            <th scope="row">{{id}}</th>
            <td>{{name}}</td>
            <td>{{phone}}</td>
            <td>{{mobile}}</td>
            <td>{{province}}</td>
            <td>{{city}}</td>
            <td>{{district}}</td>
            <td>{{addr}}</td>
            <td>{{zip}}</td>
            <td>
                <div class="default" style="display: none">默认地址</div>
                <div class="btn btn-primary change-default" onclick="setDefault({{id}})">设为默认地址</div>
                <div class="btn btn-warning" onclick="update({{id}})">修改</div>
                <div class="btn btn-danger" onclick="remove({{id}})">删除</div>
            </td>
        </tr>
        `
                template = template.replaceAll("{{id}}", item.id);
                template = template.replaceAll("{{name}}", item.name);
                template = template.replaceAll("{{phone}}", item.phone);
                template = template.replaceAll("{{mobile}}", item.mobile);
                template = template.replaceAll("{{province}}", item.province);
                template = template.replaceAll("{{city}}", item.city);
                template = template.replaceAll("{{district}}", item.district);
                template = template.replaceAll("{{addr}}", item.addr);
                template = template.replaceAll("{{zip}}", item.zip);
                let tr = document.createElement("tr");
                tr.innerHTML = template;
                if (item.dfault) {
                    tr.querySelector(".default").style.display = "inline-block";
                    tr.querySelector(".change-default").style.display = "none";
                }
                document.querySelector(".table tbody").appendChild(tr);
            });
        })

        submitBtn.addEventListener("click", () => {
            const userId = 1;
            const name = document.querySelector("#name-input").value;
            const phone = document.querySelector("#phone-input").value;
            const mobile = document.querySelector("#mobile-input").value;
            const province = document.querySelector("#province").value;
            const city = document.querySelector("#city").value;
            const district = document.querySelector("#district").value;
            const addr = document.querySelector("#addr-input").value;
            const zip = document.querySelector("#zip-input").value;

            if (userId && name && mobile && province && city && district && zip) {
                if (currentChangeType === "insert") {
                    axios.post("http://localhost:8080/addresses/save", {
                        userId: userId,
                        name: name,
                        phone: phone,
                        mobile: mobile,
                        province: province,
                        city: city,
                        district: district,
                        addr: addr,
                        zip: zip
                    }).then(res => {
                        if (res.status === 200) {
                            addressModal.hide();
                            window.location.reload();
                        }
                    })
                } else if (currentChangeType === "update") {
                    axios.put("http://localhost:8080/addresses/update", {
                        id: currentId,
                        userId: userId,
                        name: name,
                        phone: phone,
                        mobile: mobile,
                        province: province,
                        city: city,
                        district: district,
                        addr: addr,
                        zip: zip
                    }).then(res => {
                        if (res.status === 200) {
                            addressModal.hide();
                            window.location.reload();
                        }
                    })
                }
            } else {
                alert("请填写完整信息");
            }
        })
    }
</script>
</html>
